<?php

/**
 * This is the model class for table "sps_juso_attribute_changes".
 *
 * The followings are the available columns in table 'sps_juso_attribute_changes':
 * @property integer $id
 * @property integer $Kundennummer
 * @property string $Geschlecht
 * @property string $Briefanrede
 * @property string $Vorname
 * @property string $Name
 * @property string $Strasse
 * @property integer $Hausnummer
 * @property string $Hausnummer_Zusatz
 * @property string $PLZ
 * @property string $Ortschaft
 * @property string $Kanton
 * @property string $Land
 * @property string $Mobile
 * @property string $Email
 * @property string $Email_Geschaeft
 * @property string $Telefon_Privat
 * @property string $Telefon_Geschaeft
 * @property string $Geburtsdatum
 * @property string $Sprache
 * @property integer $sectionChange
 * @property integer $UserId
 * @property string $creationDate
 * @property string $userComment
 * @property string $confirmationDate
 * @property string $confirmationComment
 */
class AttributeChange extends CActiveRecord {

	public $member;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AttributeChange the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'sps_juso_attribute_changes';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Kundennummer, Geschlecht, Briefanrede, Vorname, Name, Strasse, Hausnummer, Hausnummer_Zusatz, PLZ, Ortschaft, Kanton, Land, Mobile, Email, Email_Geschaeft, Telefon_Privat, Telefon_Geschaeft, Geburtsdatum, Sprache, userId, creationDate, userComment, confirmationDate, confirmationComment, confirmationKey, sectionChange', 'safe'),
			array('Email, Email_Geschaeft', 'email'),
			array('Geschlecht, Briefanrede, Vorname, Name, Strasse, PLZ, Ortschaft, Kanton, Land, Sprache', 'required'),
			array('confirmationComment', 'required', 'on' => array('confirm/acceptModified')),
//			// The following rule is used by search().
//			// Please remove those attributes that should not be searched.
//			array('id, Kundennummer, Geschlecht, Briefanrede, Vorname, Name, Strasse, Hausnummer, Hausnummer_Zusatz, PLZ, Ortschaft, Kanton, Land, Mobile, Email, Email_Geschaeft, Telefon_Privat, Telefon_Geschaeft, Geburtsdatum, Sprache, sectionChange, UserId, creationDate, userComment, confirmationDate, confirmationComment', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', array('userId' => 'id'), 'joinType' => 'INNER JOIN'),
			'memberdata' => array(self::BELONGS_TO, 'Member', 'Benutzernummer', 'joinType' => 'INNER JOIN'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'Kundennummer' => 'OM-Nummer',
			'Benutzernummer' => 'Benutzernummer',
			'Geschlecht' => 'Geschlecht',
			'Briefanrede' => 'Briefanrede',
			'Vorname' => 'Vorname',
			'Name' => 'Name',
			'Strasse' => 'Strasse',
			'Hausnummer' => 'Nr',
			'Hausnummer_Zusatz' => 'Zusatz',
			'PLZ' => 'PLZ',
			'Ortschaft' => 'Ort',
			'Kanton' => 'Kanton',
			'Land' => 'Land',
			'Mobile' => 'Mobile',
			'Email' => 'E-Mail',
			'Email_Geschaeft' => 'E-Mail Gesch&auml;ft',
			'Telefon_Privat' => 'Telefon Privat',
			'Telefon_Geschaeft' => 'Telefon Gesch&auml;ft',
			'Geburtsdatum' => 'Geburtsdatum',
			'Sprache' => 'Sprache',
			'sectionChange' => 'Sektionswechsel',
			'userId' => 'User',
			'creationDate' => 'Datum',
			'userComment' => 'Anmerkungen',
			'confirmationDate' => 'Confirmation Date',
			'confirmationComment' => 'Confirmation Comment',
			'member_name' => 'Mitglied',
		);
	}

	public function loadMember($member) {
		$this->Kundennummer = $member->memberships[0]->Kundennummer;
		$this->Benutzernummer = $member->Benutzernummer;
		$this->Geschlecht = $member->Geschlecht;
		$this->Briefanrede = $member->profileExtensions->Briefanrede;
		$this->Vorname = $member->Vorname;
		$this->Name = $member->Name;
		$this->Strasse = $member->Strasse;
		$this->Hausnummer = $member->Hausnummer;
		$this->Hausnummer_Zusatz = $member->Hausnummer_Zusatz;
		$this->PLZ = $member->PLZ;
		$this->Ortschaft = $member->Ortschaft;
		$this->Kanton = $member->Kanton;
		$this->Land = $member->Land;
		$this->Mobile = $member->Mobile;
		$this->Email = $member->Email;
		$this->Email_Geschaeft = $member->profileExtensions->Email_Geschaeft;
		$this->Telefon_Privat = $member->Telefon_Privat;
		$this->Telefon_Geschaeft = $member->Telefon_Geschaeft;
		$this->Geburtsdatum = strpos($member->Geburtsdatum, '0-00-00') === false && strpos($member->Geburtsdatum, '0000-00-00') === false ? date("d.m.Y", strtotime($member->Geburtsdatum)) : "";
		$this->Sprache = $member->Sprache;
		$this->member = $member;
	}

	public function getOpenChangesByMember($member) {
		$criteria = new CDbCriteria();
		$criteria->compare('confirmationDate', '0000-00-00 00:00:00');
		$criteria->compare('Benutzernummer', $member->Benutzernummer);
		return $this->find($criteria);
	}

	public function getOpenChangesByUser($userId) {
		$criteria = new CDbCriteria();
		$criteria->compare('confirmationDate', '0000-00-00 00:00:00');
		$criteria->compare('userId', $userId);
		
		return new CActiveDataProvider($this, array(
					'criteria' => $criteria,
				));
	}
	
	public function getOpenChanges() {
		$criteria = new CDbCriteria();
		$criteria->compare('confirmationDate', '0000-00-00 00:00:00');
		
		return new CActiveDataProvider($this, array(
					'criteria' => $criteria,
				));
	}	

	public function compareWithMember($member) {
		$hasChanges = false;

		if ($this->Geschlecht == $member->Geschlecht) {
			$this->Geschlecht = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Briefanrede == $member->profileExtensions->Briefanrede) {
			$this->Briefanrede = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Vorname == $member->Vorname) {
			$this->Vorname = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Name == $member->Name) {
			$this->Name = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Strasse == $member->Strasse) {
			$this->Strasse = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Hausnummer == $member->Hausnummer) {
			$this->Hausnummer = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Hausnummer_Zusatz == $member->Hausnummer_Zusatz) {
			$this->Hausnummer_Zusatz = null;
		} else {
			$hasChanges = true;
		}

		if ($this->PLZ == $member->PLZ) {
			$this->PLZ = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Ortschaft == $member->Ortschaft) {
			$this->Ortschaft = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Kanton == $member->Kanton) {
			$this->Kanton = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Land == $member->Land) {
			$this->Land = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Mobile == $member->Mobile) {
			$this->Mobile = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Email == $member->Email) {
			$this->Email = null;
		} else {
			$hasChanges = true;
		}

		$member->profileExtensions->Email_Geschaeft = trim($member->profileExtensions->Email_Geschaeft);
		if ($this->Email_Geschaeft == $member->profileExtensions->Email_Geschaeft) {
			$this->Email_Geschaeft = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Telefon_Privat == $member->Telefon_Privat) {
			$this->Telefon_Privat = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Telefon_Geschaeft == $member->Telefon_Geschaeft) {
			$this->Telefon_Geschaeft = null;
		} else {
			$hasChanges = true;
		}

		if (date('Y-m-d', strtotime($this->Geburtsdatum)) == $member->Geburtsdatum || $this->Geburtsdatum == '') {
			$this->Geburtsdatum = null;
		} else {
			$hasChanges = true;
		}

		if ($this->Sprache == $member->Sprache) {
			$this->Sprache = null;
		} else {
			$hasChanges = true;
		}

		return $hasChanges;
	}

	public function beforeSave() {

		$hasChanges = false;

		if ($this->isNewRecord) {
			$this->userId = Yii::app()->user->getId();
			$this->confirmationKey = UUID::generate();
			$hasChanges = $this->compareWithMember($this->member);
		} else {
			$hasChanges = true;
		}
		if ($hasChanges) {
			return parent::beforeSave();
		}
		return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('Kundennummer', $this->Kundennummer);
		$criteria->compare('Geschlecht', $this->Geschlecht, true);
		$criteria->compare('Briefanrede', $this->Briefanrede, true);
		$criteria->compare('Vorname', $this->Vorname, true);
		$criteria->compare('Name', $this->Name, true);
		$criteria->compare('Strasse', $this->Strasse, true);
		$criteria->compare('Hausnummer', $this->Hausnummer);
		$criteria->compare('Hausnummer_Zusatz', $this->Hausnummer_Zusatz, true);
		$criteria->compare('PLZ', $this->PLZ, true);
		$criteria->compare('Ortschaft', $this->Ortschaft, true);
		$criteria->compare('Kanton', $this->Kanton, true);
		$criteria->compare('Land', $this->Land, true);
		$criteria->compare('Mobile', $this->Mobile, true);
		$criteria->compare('Email', $this->Email, true);
		$criteria->compare('Email_Geschaeft', $this->Email_Geschaeft, true);
		$criteria->compare('Telefon_Privat', $this->Telefon_Privat, true);
		$criteria->compare('Telefon_Geschaeft', $this->Telefon_Geschaeft, true);
		$criteria->compare('Geburtsdatum', $this->Geburtsdatum, true);
		$criteria->compare('Sprache', $this->Sprache, true);
		$criteria->compare('sectionChange', $this->sectionChange);
		$criteria->compare('UserId', $this->UserId);
		$criteria->compare('creationDate', $this->creationDate, true);
		$criteria->compare('userComment', $this->userComment, true);
		$criteria->compare('confirmationDate', $this->confirmationDate, true);
		$criteria->compare('confirmationComment', $this->confirmationComment, true);

		return new CActiveDataProvider($this, array(
					'criteria' => $criteria,
				));
	}

}